function showAtrributes(event){  
    var out='';    
    for(var p in event){    
        if(typeof(p)!="function"){    
            out+=p+"="+event[p]+"  ";    
        }    
    }    
    alert(out);   
}  
var key='F4bfb7ec82f386cf8541158ad5801138';  
var map = new BMap.Map("map"); // 创建地图实例     
var point = new BMap.Point(116.404, 39.915); // 创建点坐标     
map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别    


map.addControl(new BMap.NavigationControl());     //控制地图的平移和缩放的功能 
map.addControl(new BMap.ScaleControl());       

map.setDefaultCursor("crosshair");  
    

map.enableScrollWheelZoom();//滚轮缩放事件    
//map.enableKeyboard(); //键盘方向键缩放事件    
map.enableContinuousZoom(); // 开启连续缩放效果    
//map.enableInertialDragging();　// 开启惯性拖拽效果     
  
preMarker='';  
  
//点击地图选址  
map.addEventListener("click", function(e){   //点击事件    
//alert(e.point.lng + ", " + e.point.lat);  
    if(!e.overlay){  
        document.getElementById("mapx").innerHTML="鼠标当前x位置:"+e.point.lng;    
        document.getElementById("mapy").innerHTML="鼠标当前y位置:"+e.point.lat;    
        document.getElementById("level").innerHTML="缩放等级:"+this.getZoom();  
        var targetUrl='http://api.map.baidu.com/geocoder/v2/?ak='+key+'&location='+e.point.lat+','+e.point.lng+'&output=json&pois=0';  
        $.ajax({  
            url:targetUrl,  
            type:"get",  
            async : false,  
            dataType:'jsonp',  
            beforeSend:function(){  
              //alert(targetUrl);    
            },  
            success:function(data,status){  
                //alert(status);  
                if(status=='success' && data.status==0){  
                      
                    //alert(JSON.stringify(data));  
                    //location.href=a.attr("href");  
                      
                    var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), {  
                        offset: new BMap.Size(10, 25), // 指定定位位置  
                        imageOffset: new BMap.Size(0, 0 - 10 * 25) // 设置图片偏移  
                    });  
                    var marker=new BMap.Marker(e.point,{icon:myIcon});  
                    map.removeOverlay(preMarker);  
                    map.addOverlay(marker);  
                    content="<div>地址:"+data.result.formatted_address+"</div>";  
                    content+='<form action="around.php" method="post"><input type="hidden" name="lng" value="'+data.result.location.lng+'"><input type="hidden" name="lat" value="'+data.result.location.lat+'"><input type="submit" value="查看附近幼儿园"></form>';  
                    var info=new BMap.InfoWindow(content);  
                    marker.openInfoWindow(info);                   
                    preMarker=marker;  
                }  
            },  
            error:function(XMLHttpRequest, textStatus, errorThrown){  
                alert(XMLHttpRequest.status);  
                alert(XMLHttpRequest.readyState);  
                alert(textStatus);  
                alert(errorThrown);  
            }  
        });  
          
    }  
});      
    
    
map.addEventListener("dragend", function(){   //拖拽事件    
 var center = map.getCenter();       
 document.getElementById("mapx").innerHTML="拖拽后中心x位置:"+center.lng;    
 document.getElementById("mapy").innerHTML="拖拽后中心y位置:"+center.lat;    
 document.getElementById("level").innerHTML="缩放等级:"+this.getZoom();    
 //alert("地图中心点变更为：" + center.lng + ", " + center.lat);       
});     
    
map.addEventListener("zoomend", function(){   //缩放事件    
 //alert("地图缩放至：" + this.getZoom() + "级");       
});  
  
/*自定义搜索2*/    
function serachlocal(){  
    var markerArray=new Array();  
    var typeArray=new Array('','－公交站','','－地铁站');  
    var local = new BMap.LocalSearch(map, {       
    renderOptions: {       
        map: map,       
        //panel: "results",//结果容器id    
        autoViewport: true,   //自动结果标注    
        selectFirstResult: false   //不指定到第一个目标    
    },       
    pageCapacity: 8,  
    //自定义marker事件  
    onMarkersSet:function(pois){  
        for(var i=0;i<pois.length;i++){  
            (function(){  
                var index=i;  
                var curPoi=pois[i];  
                var curMarker=pois[i].marker;  
                markerArray[i]=curMarker;  
                  
                content="<h3>"+curPoi.title+typeArray[curPoi.type]+"</h3>";  
                content+="<div>地址:"+curPoi.address+"</div>";  
                content+='<form action="around.php" method="post"><input type="hidden" name="lng" value="'+curPoi.point.lng+'"><input type="hidden" name="lat" value="'+curPoi.point.lat+'"><input type="submit" value="查看附近幼儿园"></form>';  
                  
                curMarker.addEventListener('click',function(event){  
                    //showAtrributes(event);  
                    var info=new BMap.InfoWindow(content);  
                    curMarker.openInfoWindow(info);  
                    var position=curMarker.getPosition();  
                    document.getElementById("mapx").innerHTML="拖拽后中心x位置:"+position.lng;    
                    document.getElementById("mapy").innerHTML="拖拽后中心y位置:"+position.lat;    
                    //document.getElementById("level").innerHTML="缩放等级:"+this.getZoom();    
                      
                });  
            })();  
        }  
          
    },  
    //自定义搜索回调数据  
    onSearchComplete:function(results){  
        if(local.getStatus() == BMAP_STATUS_SUCCESS){  
              
            var html='<ol style="list-style: none outside none; padding: 0px; margin: 0px;">';  
            for(var i=0;i<results.getCurrentNumPois();i++){  
                var poi=results.getPoi(i);  
                var bYposition=2-i*20;  
              html+='<li id="poi'+i+'" index="'+i+'" style="margin: 2px 0px; padding: 0px 5px 0px 3px; cursor: pointer; overflow: hidden; line-height: 17px;">';  
                html+='<span style="background:url(http://api.map.baidu.com/bmap/red_labels.gif) 0 '+bYposition+'px no-repeat;padding-left:10px;margin-right:3px"></span>'  
                html+='<span style="color:#00c;text-decoration:underline"><b>'+poi.title+'</b>'+typeArray[poi.type]+'</span>';  
                html+='<span style="color:#666;">-'+poi.address+'</span>'  
                html+='</li>';  
            }  
            html+="</ol>";  
            $("#results").html(html);  
              
            for(var i=0;i<results.getCurrentNumPois();i++){  
                (function(){  
                    var index=i+1;  
                    var poi=results.getPoi(i);  
                    content="<h3>"+poi.title+typeArray[poi.type]+"</h3>";  
                    content+="<div>地址:"+poi.address+"</div>";  
                    content+='<form action="around.php" method="post"><input type="hidden" name="lng" value="'+poi.point.lng+'"><input type="hidden" name="lat" value="'+poi.point.lat+'"><input type="submit" value="查看附近幼儿园"></form>';  
                    var info=new BMap.InfoWindow(content)  
                    $("#poi"+i).click(function(){  
                        markerArray[$(this).attr('index')].openInfoWindow(info);  
                    });  
                })();  
                  
            }  
              
        }  
    },  
    });  
      
    local.search(document.getElementById("searchtext").value);    
}

function drag(obj,e){    
    var e=e ? e : event;    
    var mouse_left=e.clientX-obj.offsetLeft;    
    var mouse_top=e.clientY-obj.offsetTop;    
    var docmousemove=document.onmousemove;    
    var docmouseup=document.onmouseup;    
    document.onselectstart=function(e){return false}    
    document.onmouseup=function(){    
        document.onmousemove=docmousemove;    
        document.onmouseup=docmouseup;    
        document.onselectstart=function(e){return true}    
    }    
    document.onmousemove=function(ev){    
        var ev=ev ? ev : event;    
        if(ev.clientX-mouse_left>0&&document.documentElement.clientWidth-(ev.clientX-mouse_left)-obj.offsetWidth+document.documentElement.scrollLeft>0){    
            obj.style.left=(ev.clientX-mouse_left)+"px";    
        }    
        if(ev.clientY-mouse_top>0&&document.documentElement.clientHeight-(ev.clientY-mouse_top)-obj.offsetHeight+document.documentElement.scrollTop>0){    
            obj.style.top=(ev.clientY-mouse_top)+"px";    
        }    
    }    
} 

